package com.ljw.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.ljw.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000122610610";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCRrzSWzpPX8i6EhsuiR3BAyFMfckYnt80f6fLXPv5aYikJs/GNABPb60HOtDJ6VtzqbCxlZ7vxve+iCZkVGUkA00rmGqMN6Nn2ux1eovQmLrrGnLJr50Syxv8jjbPLLpXtWxCSfM0Tf3kCRvQxUPeYGDkCZBDT55O9WX6+86IAEO4R3SO3wdJXdLqqSqXziPeUBdsRt3L+2LvMcTzQgtOaVSUQt5ldeEDzwY/E61fjpt4+opdEl1TB8RrqxQqPmoKdknuHYY3SpOFsNtgwQBUW9l/1LSNHDRYeKwqvV9+g6EzODvGjAUhYKDV4ctsUgetVQjb6FV4Ja2jvGj5bq8T1AgMBAAECggEAZcnCtrOzPKhrtBDyann7hmfxHAGd3tZfLbMqBxZuZPw8rBjb5qZtObUQD7p7olrak7fs9RUbjfYlXlPSb5lxFnCK3mcyEd/lmqx2As9cQulXerm1OAsoRrkfPtodCxOLkYGU/KTSLJNeGyc0KtjlhZE3E+KSl1wwL5Y+JnDXea9MQbSifUdMEukmdu/NURZRP6AY/QsyY1NSR28QPG/BJJ320BF9AHKgPINCQ6MEt7hwZxROIlc22G5JiCTZrbjKFm9LbECOJ2NbFmcIVb03rS8JTI3I2Sc3xFunT14T1Bwqd6v9wYd+4O/Rhwt7/RLW/EnnTwkRb188Orgslc68QQKBgQDMB0bdjJJt3oaYcL3j5SW36qYfYc5no+TGz5BTTHgnr9mzjHsrr9B8OG6STiyMknEBZRKKaDPe5QbIP4w9DJf0BR27Ma+5csnc1A2+1GepvAtNJmxhepTnu91zTdMd4RRn7kpMrIX3NoWIoj3w5dR0KcyWeyNW83QsBXfc2D39LQKBgQC2y05XsMhb1tBzSkDj6KQcTlxAlXcQQtMvr7UpL5lk8exBrpb+EK49HcqG5s9isvxpW+mVsjzsIacZnlaz+zJ1pI7Q7juG+f63vtNPhH4ZP06kUITn6UAOYo1lDCEIoK/0GqtsEvnvCKnOa8xOZhl9TVWa/UWM+0hzhfL7HQ4T6QKBgQCIqlsqri2rkMJBvEtGK0kfr/sEzGQRAjSNg52eXnuhweYmofQjaEpT2w0dzWWD59fwYj6QHYqZRHpNiluqzRO/7eljab3Wj/BtjlGCwQ6LIJ3FWPaCOTUjILEOulOvEQS1NWXphCrcVFYQFvU8twafpRwYtHUQmlT349XNRcAIJQKBgGkDL0StaM1qAWLaW6ES+4TPd6wascYDvfN9S+QXVlZprUKmnFPlHVQ7Eyig3K9lCCE3DmBTfzBgvPC4yDKpsvlmyi05T7VhVXYWCKt8AD1c4z7+ivv4QjaBY52TdD7SoYLo92YlGLS8E2FlCIBZo3cawH1D13sAuUS0xTyCGp5RAoGBAI+ePhnF7KBJdloI+W40byYMUNLxDbbja4GxII7/U5m3EjW/5ST3H5s58iStgZW6h3l4+UAiUwKbGj0Df6KjGwpejOC4M8eGNtaudMp0ajzYTdYA1bGS9uMKf4Yl7KfEW1fc3lDxcogiPxXgC8LapWTJcyFBUlIb0CGAm6vbwTpn";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA88sXdShThcvYFL3AE0rsk31pXMlEX5KEXCi1YlJaW6JqnEeCyhZoc8XaYRokCTF+oPvKAY3ABhwvd3htNpb8qonLIaVHNKBMQhRFGh1tT0xB2IqonCt+7YHG4AZi0nv6uYI5OeFA9SqouQoi6EA9auGJkAcZ/SiYxdLqpdA5ibxwnGCJxR2vYvc6MZ3F+krRT62sAGRHeqRmIm1APMcMf4r9cWsYVmIWv9CJyfwGupYUkrovXeO1oNLQCMO3h5dh/UwYAIWSIICgr3bOBlsvNvNdGTIrQyUREOIgAu5ak1mAqeshkfvtRRN4UjmIl3jQQ52MmJfo7oN09ESQsk784QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://e3be76831ae63167.natapp.cc:35784/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
